/*
ECS386: Introduction to embedded systems
Spirometer Group
Members:
1) Keerati Suibkitwanchai ID:5422780395
2) Siwalee Choilek ID:5422780536
*/

///////////////////////////////Volume Measure////////////////////////////////////////////////////

const int pressurePin = A0;     //pin AO is connected to output voltage
float volume = 0;               //initial volume is set as zero.

void setup(){ 
  Serial.begin(9600);           //Begin serial communication with baud rate 9600.
  pinMode(pressurePin, INPUT);  //Set the pressure pin as an input.
}

void loop(){ 
  float inputValue = analogRead(pressurePin); //read the input value from the pressure pin (analog from 0 to 1023).
  float voltage = (inputValue/1024.0)*5.0;    //convert the input value to voltage ranged from 0 to 5 volt.
  float pressure = (voltage/5.0)*10000.0;     //convert the voltage to the differential pressure ranged from 0 to 10 kPa (sensor MPXV5010DP)
  
  //Serial.println(pressure);
  //The offset value is between 425 and 445 Pascal
  if((int(pressure)==439||int(pressure)==429||int(pressure)==449)){ //if pressure is out of detection criteria
    volume = 0;
    Serial.print("Volume: ");
    Serial.println(volume);
    Serial.print("Pressure:");
    Serial.println(pressure);
  }
  else {
    volume = volume + (pressure-439)/800.0; //lung luvolume is calculated
    Serial.print("Volume: ");
    Serial.println(volume);
    Serial.print("Pressure:");
    Serial.println(pressure); 
  }
  


  delay(20); //delay in every 20 milliseconds
}
